#coding:utf-8
from .. import model,db
from flask import render_template,redirect,request,url_for,session

from . import auth
from ..model import Users,Kinds,Courses,Videos,Teachers
import random,datetime
# @auth.route('/h5')
# def h5():
#     return render_template('login.html')


# 注册
@auth.route('/add',methods=['POST'])
def add():
    name = request.form['name']
    psw = request.form['psw']
    confirm = request.form['confirm']
    user = db.session.query(Users).filter(Users.name == name).first()
    if not user and psw == confirm:
        userss = Users(name = name,password = psw)
        db.session.add(userss)
        db.session.commit()
    return redirect(url_for('main.login'))


# 登录
@auth.route('/verify',methods=['POST'])
def verify():
    name = request.form['name']
    psw = request.form['psw']
    user = db.session.query(Users).filter(Users.name==name).first()
    if user and user.verify_psw(psw):       
        session['state']=True
        session['name'] = name
        return redirect(url_for('main.h5'))
    else:
        return redirect(url_for('main.login'))


# 注销

@auth.route('/logout')
def logout():
    session['state'] = False
    session.pop('name')
    return redirect(url_for('main.h5'))


@auth.route('/adds')
def adds():
    for x in xrange(1,10):
        name = 't_%s'%x
        adstract = 'this is t_%s adstract'%x
        mun = random.randint(0,1)
        if mun == 0:
            level = '助教'
        else:
            level = '讲师'
        teachers = Teachers(name = name,adstract = adstract,level = level)
        db.session.add(teachers)
        db.session.commit()

    # for item in range(1,8):
    #     name = 'name_%s' %item
    #     kind = model.Kinds(name=name)
    #     db.session.add(kind)
    #     db.session.commit()

    # for item in range(1,20):
    #     title = 'title_%s' %item 
    #     abstract = '_abstract' * random.randint(1,6)
    #     addtime = datetime.datetime.now()
    #     # 外键范围，从数据库查看，然后在设置
    #     k_id = random.randint(1,7)
    #     course = model.Courses(title=title,abstract=abstract,addtime=addtime,k_id=k_id)
    #     db.session.add(course)
    #     db.session.commit()

    # for item in xrange(1,60):
    #     title = 'title_%s' %item 
    #     time = random.randint(400,600)
    #     hits = random.randint(1000,2000)
    #     addtime = datetime.datetime.now()
    #     adstract = 'adstract'*random.randint(6,10)
    #     t_id  = random.randint(1,8)
    #     c_id = random.randint(1,19)
    #     video = model.Videos(title=title,time=time,hits=hits,addtime=addtime,adstract=adstract,t_id=t_id,c_id=c_id)
    #     db.session.add(video)
    #     db.session.commit()



    return '添加成功'


# 删除Courses中的数据
@auth.route('/commit_delete_course',methods = ['POST'])
def delete_course():

    course = db.session.query(model.Courses).filter(model.Courses.id == request.form['id']).first()
    db.session.delete(course)
    db.session.commit()
    return 'success'
# 删除Videos中的数据
@auth.route('/commit_delete_video',methods = ['POST'])
def delete_video():
    video = db.session.query(model.Videos).filter(model.Videos.id == request.form['id']).first()
    db.session.delete(video)
    db.session.commit()
    return 'success'

#删除Teachers中的数据
@auth.route('/commit_delete_teacher',methods=['POST'])
def delete_teacher():
    teacher = db.session.query(model.Teachers).filter(model.Teachers.id == request.form['id']).first()
    db.session.delete(teacher)
    db.session.commit()
    return 'success'



# # 删除测试
# @auth.route('/delete1')
# def delete1():
#     kind1 = db.session.query(Kinds).first()
#     db.session.delete(kind1)
#     db.session.commit()

#     return '1'



# 提交course修改

@auth.route('/update_course/<id>',methods = ['POST'])
def update_course(id):
    course = db.session.query(Courses).filter(Courses.id == id).first()
    course.title = request.form['title']
    course.abstract = request.form['abstract']
    db.session.commit()
    kind = course.kinds
    name = kind.id

    return redirect('/course/%s'%name)


#提交video修改

@auth.route('/update_video/<id>',methods = ['POST'])
def update_video(id):
    video = db.session.query(Videos).filter(Videos.id == id).first()
    teacher = db.session.query(Teachers).filter(Teachers.name == request.form['name']).first()
    video.title = request.form['title']
    video.hits = request.form['hits']
    video.t_id = teacher.id
    video.adstract = request.form['adstract']
    db.session.commit()
    course = video.courses
    id = course.id

    return redirect('/videos/%s'%id)

#提交teacher修改
@auth.route('/update_tcher/<id>',methods = ['POST'])
def update_tcher(id):
    teacher = db.session.query(Teachers).filter(Teachers.id == id).first()
    teacher.name = request.form['name']
    teacher.adstract = request.form['adstract']
    teacher.level = request.form['level']
    db.session.commit()
    return redirect(url_for('main.teacher'))



# 添加course
@auth.route('/add_cou/<id>',methods=['POST'])
def addcourse(id):
    title = request.form['title']
    abstract = request.form['abstract']
    addtime = datetime.datetime.now()
    k_id = id
    course = model.Courses(title=title,abstract=abstract,addtime=addtime,k_id=k_id)
    db.session.add(course)
    db.session.commit()
    return redirect('/course/%s'%id)
    



# 添加video
@auth.route('/add_vid/<id>',methods=['POST'])
def addvideo(id):
    title = request.form['title']
    adstract = request.form['abstract']
    time = request.form['time']
    c_id = id
    teacher = db.session.query(Teachers).filter(Teachers.name == request.form['name']).first()
    t_id = teacher.id
    hits = 0
    addtime = datetime.datetime.now()
    video = model.Videos(title = title,time =time,hits = hits,addtime = addtime,adstract =adstract,t_id = t_id,c_id = c_id)
    db.session.add(video)
    db.session.commit()
    return redirect('/videos/%s'%id)

#添加teacher
@auth.route('/add_teacher',methods=['POST'])
def addteacher():
    name = request.form['name']
    adstract = request.form['adstract']
    level = request.form['level']
    teacher = model.Teachers(name = name,adstract = adstract,level = level)
    db.session.add(teacher)
    db.session.commit()
    return redirect(url_for('main.teacher'))